<template>
    <div>
        <el-button type="primary" @click="dialogVisible = true">添加标签</el-button>
        <el-table :data="tableData" style="width: 100%">
            <el-table-column label="阶段" width="180" :sortable="true" :sort-method="sortFn">
                <template #default="scope">
                    <el-tag class="ml-2" :type="tags[scope.row.grade - 1]">{{ names[scope.row.grade - 1] }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="title" label="标签名" width="180" />
            <el-table-column label="操作">
                <template #default="scope">
                    <div style="display: flex; align-items: center">
                        <el-button type="primary" round @click="handleUpdata(scope.row)"
                            :disabled="scope.row.default">编辑</el-button>
                        <el-popconfirm title="确定要删除吗?" confirm-button-text="确定" cancel-button-text="取消"
                            @confirm="handleDelete(scope.row)">
                            <template #reference>
                                <el-button type="danger" round :disabled="scope.row.default">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加标签 -->
        <el-dialog v-model="dialogVisible" title="添加标签">
            <el-form :model="addForm" status-icon label-width="80">
                <el-form-item label="标签名" prop="title">
                    <el-input v-model="addForm.title" />
                </el-form-item>
                <el-form-item label="阶段" prop="grade">
                    <el-select v-model="addForm.grade" class="m-2" placeholder="请选择标签" size="large" style="width:100%">
                        <el-option v-for="(item, index) in names" :key="index" :label="item" :value="index + 1" />
                    </el-select>
                </el-form-item>

            </el-form>

            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleAddConfirm()">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
        <!-- 更新/编辑标签 -->
        <el-dialog v-model="updataDialogVisible" title="编辑标签">
            <el-form :model="addForm" status-icon label-width="80">
                <el-form-item label="标签名" prop="title">
                    <el-input v-model="updataForm.title" />
                </el-form-item>
                <el-form-item label="阶段" prop="grade">
                    <el-select v-model="updataForm.grade" class="m-2" placeholder="请选择标签" size="large" style="width:100%">
                        <el-option v-for="(item, index) in names" :key="index" :label="item" :value="index + 1" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="updataDialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleUpdataConfirm()">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import axios from 'axios'
const tableData = ref([])
const rolelist = ref([])
const names = ['第一阶段', '第二阶段', '第三阶段',]
const tags = ['success', 'warning', 'danger']
onMounted(async () => {
    getList()
})

const getList = async () => {
    let res = await axios.get('/adminapi/tags')
    tableData.value = res.data
}
// 添加用户
const dialogVisible = ref(false)
const addForm = reactive({
    title: '',
    grade: '',
})
// add确定
const handleAddConfirm = async () => {
    console.log(addForm)
    dialogVisible.value = false
    // 添加用户数据
    await axios.post('/adminapi/tags', addForm)
    await getList()
    addForm.title = ''
    addForm.grade = ''
}


// 编辑用户
const updataForm = reactive({
    grade: '',
    title: '',
})
const updataDialogVisible = ref(false)
const currentItem = ref({})
const handleUpdata = (item) => {
    updataDialogVisible.value = true
    updataForm.grade = item.grade
    updataForm.title = item.title

    currentItem.value = item
}

// 编辑确定
const handleUpdataConfirm = async () => {
    updataDialogVisible.value = false
    // // 编辑用户数据
    await axios.put(`/adminapi/tags/${currentItem.value._id}`, updataForm)
    await getList()
}

// 删除用户
const handleDelete = async (item) => {
    await axios.delete(`/adminapi/tags/${item._id}`)
    await getList()
}

// 排序
const sortFn = (a, b) => {
    return a.grade - b.grade
}
</script>